<?php
namespace app\restaurantadmin\controller;
use think\Controller;
class Notice extends Controller
{
    public function notice()
    {
        $uid=session('id');
        $shop= \think\Db::name('dining')->where('admin_id',$uid)->find();
        $shopid=$shop['shop_id'];
        // var_dump($shopid);die;

        $noticelist= \think\Db::name('notice')->where('did',$shopid)->group('id desc')->select();
        // var_dump($foodtypes);die;
        $this->assign('noticelist',$noticelist);

        return $this->fetch();


    }

    //发布公告信息
    public function addnotice(){
        $uid=session('id');
        // echo("<script type='text/javascript'> alert($uid);</script>");
        $shop= \think\Db::name('dining')->where('admin_id',$uid)->find();
        $shopid=$shop['shop_id'];
        // var_dump($shopid);die;

        if(request()->isPost()){
            $data=[
                'notice'=>input('notice'),
                'did'=>$shopid,
                'ndate'=>date('Y-m-d H:m:s'),
            ];
            // var_dump($data) ;die;
            //添加数据前应该先验证
            $validate = \think\Loader::validate('Noticeval');

            if($validate->check($data)){
                $db=\think\Db::name('notice')->insert($data);
                if($db){
                    return $this->redirect('Notice/notice');
                }
                else{
                    echo("<script type='text/javascript'> alert('添加失败！');</script>");
                }
            }else{
                // echo $validate->getError();
                $this->error($validate->getError());
                // $error=$validate->getError();
                // echo("<script type='text/javascript'> alert($error);</script>");
            }

        }
    }

    //删除公告信息
    public function notice_delete(){
        $id=input('id');
        if(db('notice')->delete($id)){
            return $this->redirect('Notice/notice');
        }else{
            return $this->error('删除失败！');
        }

    }

    //批量删除公告信息
    public function delchecks(){

        $ck=$_GET["id"];

        if(db('notice')-> where("id in ($ck)")->delete()){
            return $this->redirect('Notice/notice');
        }else{
            return $this->error('删除失败！');
        }


    }

    //公告信息notice_edit修改页面显示
    public function notice_edit(){
        $id=input('id');
        // echo $id;die();
        $notice= \think\Db::name('notice')->where('id',$id)->find();
        // dump($notice);die();
        $this->assign('notice_one',$notice);

        return $this->fetch();



    }

    //修改公告信息
    public function noticeedit(){
        $id=input('id');//获取id
        // echo $id;die();

        if($db=\think\Db::name('notice')->where('id',$id)->update(['notice' =>input('notice')])){
            return $this->redirect('Notice/notice');
        }else{
            return $this->error('修改失败！');
        }
        return;
    }







}


?>

